package ex 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author ...
	 */
	public class Circle 
	{
		public var radius : Number;
		public var center : Point;
		public function Circle(x:Number, y:Number, r:Number)
		{
			radius = r;
			center = new Point(x, y);
		}
		
		public function get diameter () : Number
		{
			return radius * 2;
		}
		public function set diameter (value:Number) : void
		{
			radius = value / 2;
		}
		
		
	/*	public function Circle(c:Point,r:Number) 
		{
			radius = r;
			center = c.clone();
		}
	*/
		public function contains (x:Number, y:Number) : Boolean
		{
			return (center.x - x) * (center.x - x) + (center.y - y) * (center.y - y ) < radius * radius;
		}
		public function containsPoint (point:Point) : Boolean
		{
			return contains(point.x, point.y);
		}
		public function containsRect (rect:flash.geom.Rectangle) : Boolean
		{
			return containsPoint(rect.topLeft) && containsPoint(rect.bottomRight);//Possibly wrong
		}
		
		public function containsCircle (circle:Circle) : Boolean
		{
			return circle.center.subtract(center).length + circle.radius < radius;
		}
		
		public function equals (toCompare:Circle) : Boolean
		{
			return toCompare.radius == radius && toCompare.center.equals(center);
		}
		
		public function intersects (toIntersect:Circle) : Boolean
		{
			var dx:int = toIntersect.center.x - center.x;
			var dy:int = toIntersect.center.y - center.y;
			var r:int = toIntersect.radius + radius;
			return dx * dx + dy * dy < r * r;
		}
		
		public function bounds():flash.geom.Rectangle
		{
			return new flash.geom.Rectangle(center.x - radius, center.y - radius, diameter, diameter);
		}
		static function dot(A:Point, B:Point):Number
		{
			return A.x * B.x + A.y * B.y;
		}
		
		static public function reflect(N:Point, L:Point):Point
		{
			N.normalize(1);
			var r:Point = L.clone();
			var d:Number = dot(N, L);
			if (d < 0)
			{
				r.x += -d * 2 * N.x;
				r.y += -d * 2 * N.y;
				return r;
			}
			else
			{
				//Nothing to reflect
				return r;
			}
			
		}
	}

}
